package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsSite;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.LogisticsState;
import com.yidu.logisticssystem.entity.vo.StateVo;
import com.yidu.logisticssystem.service.*;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 货运状态(LogisticsState)表控制层
 *
 * @author lzx
 * @since 2021-03-08 16:46:57
 */
@RestController
@RequestMapping("logisticsState")
public class LogisticsStateController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsStateService logisticsStateService;



    @Resource
    private LogisticsMapService mapService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsState selectOne(Integer id) {
        return this.logisticsStateService.queryById(id);
    }



    /**
     * 分页查询所有
     * @return
     */

    @ApiOperation(value = "查询所有的订单和状态",notes = "返回参数")
    @GetMapping("selectdd")
    @Transactional
    public Map<String,Object> selectdd(StateVo vo, HttpServletRequest request){
        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");
        String areaid=staff.getMapId(); //地区编号
        vo.setAreaId(areaid);
        //将参数传入分页助手 PageHelper
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        Map<String,Object> map = new HashMap<>();
        List<StateVo> list =logisticsStateService.selecthyd(vo);
        for (StateVo stateVo:list) {
            int dz1=stateVo.getWaySendarea().indexOf(",");//根据发货地址 截取 , 数量
            int dz2=stateVo.getWayConsignarea().indexOf(",");//收货货地址
            LogisticsMap map1=mapService.queryById(stateVo.getWaySendarea().substring(0,dz1));//发货地址
            LogisticsMap map2=mapService.queryById(stateVo.getWayConsignarea().substring(0,dz2));//收货货地址
            stateVo.setWaySendarea(map1.getMapName()+stateVo.getWaySendarea().substring(dz1+1)); //页面显示收货
            stateVo.setWayConsignarea(map2.getMapName()+stateVo.getWayConsignarea().substring(dz2+1));//页面显示发货
        }
        //分页  new  PageInfo
        PageInfo<StateVo> pageInfo = new PageInfo<>(list);
        // map
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);//所有数据
        map.put("count",pageInfo.getTotal());//总行数

        return map;
    }




    @ApiOperation(value = "添加订单状态",notes = "返回自动增长id")
    @GetMapping("insert")
    public int add(LogisticsState state){
        logisticsStateService.insert(state);
        return state.getZiId();
    }


    /**
     * 分页查询所有
     * @return
     */

    @ApiOperation(value = "根据地区查询所有发货订单",notes = "返回参数")
    @GetMapping("selectfahuo")
    public Map<String,Object> selectfahuo(StateVo vo, HttpServletRequest session){
      LogisticsStaff staff = (LogisticsStaff)session.getSession().getAttribute("staff"); //取得登陆者对象 staff
        vo.setAreaId(staff.getMapId()); //将登陆者 的工作地区传入 vo
        //将参数传入分页助手PageHelper
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        Map<String,Object> map = new HashMap<>();//定义 map
        List<StateVo> list =logisticsStateService.selectfahuo(vo);// 根据地区查发货地址
        for (StateVo stateVo:list) {
            int dz2=stateVo.getWayConsignarea().indexOf(",");//截取收货地址分界点
            LogisticsMap map2=mapService.queryById(stateVo.getWayConsignarea().substring(0,dz2));//根据地区编号查找地区消息
            stateVo.setWayConsignarea(map2.getMapName()+stateVo.getWayConsignarea().substring(dz2+1));//地区名称+vo中收货地址的分界点后面
        }
        PageInfo<StateVo> pageInfo = new PageInfo<>(list);//分页 定义PageInfo
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);//所有数据
        map.put("count",pageInfo.getTotal());//总行
        return map;
    }



    /**
     * 分页查询所有
     * @return
     */

    @ApiOperation(value = "根据地区查询所有发货订单",notes = "返回参数")
    @GetMapping("selectkdyps")
    public Map<String,Object> selectkdyps(StateVo vo, HttpServletRequest session){
        return logisticsStateService.selectkdyps(vo,session);
    }

    /**
     * 根据货运订单查询已完成的状态
     * @param state
     * @return
     */
    @ApiOperation(value = "根据货运订单查询已完成的状态",notes = "返回参数")
    @GetMapping("findOk")
    public BaseMessage find(LogisticsState state){
        List<LogisticsState> list=logisticsStateService.findOk(state);
        return BaseMessage.succes(list);
    }


    /**
     * 分页查询所有
     * @return
     */
    @ApiOperation(value = "根据地区查询所有收货订单",notes = "返回参数")
    @GetMapping("selectshdz")
    public Map<String,Object> selectshdz(StateVo vo, HttpServletRequest request){
        System.out.println(vo.getShdz());
        return logisticsStateService.selectshdz(vo,request);
    }


    /**
     * 根据货运订单查询当前状态
     * @param state
     * @return
     */
    @ApiOperation(value = "根据货运订单查询当前状态",notes = "返回参数")
    @GetMapping("findNo")
    public BaseMessage findNo(LogisticsState state){
        List<LogisticsState> list=logisticsStateService.findNo(state);
        return BaseMessage.succes(list);
    }
}
